<?php

class ActionRegister extends BaseAction
{
    public function run()
    {
        $this->isRender = false;
        
        if ( Yii::app()->request->getIsPostRequest() )
        {
            $oAddress = new Address();
            $oAddress->setAttributesFromRequest('aAddress');
            
            $oProfile = new Profile('register');
            $oProfile->setAttributesFromRequest('aProfile');
            
            $oProfileLegalInfo = new ProfileLegalInfo();
            $oProfileLegalInfo->setAttributesFromRequest('aProfileLegalInfo');
            
            $oTariffPlan = new TariffPlan();
            $oTariffPlan->setAttributesFromRequest('aTariffPlan');
            $oTariffPlan->setTariffPlanAttributes();
            
            if ( $oAddress->save() && $oProfile->save() && $oProfileLegalInfo->save() )
            {
                $sHash                        = substr(md5(Yii::app()->params['password_sold'].$oProfile->email), 0, 8);
                $oUser                        = new User();
                $oUser->user_login            = rand(1000000000, 9999999999);
                $oUser->user_password         = md5($sHash);
                $oUser->user_cdate            = time();
                $oUser->profile_id            = $oProfile->profile_id;
                $oUser->address_id            = $oAddress->address_id;
                $oUser->profile_legal_info_id = $oProfileLegalInfo->profile_legal_info_id;
                $oUser->status                = 'pending';
                $oUser->save();

                $oTariffPlan->user_id = $oUser->user_id;
                $oTariffPlan->status = 'pending';
                $oTariffPlan->is_first = 1;
                $oTariffPlan->save();

                Yii::app()->authManager->assign('user', $oUser->user_id);

                $oEmailNotification = new EmailNotification();
                $oEmailNotification->sendEmailToAdminAfterRegister($oUser, $oProfile, $oTariffPlan);
                $oEmailNotification->sendEmailToUserAfterRegister($oProfile, $oTariffPlan);
            }
        }
    }
}